/**
 * 27/ott/2011
 */
package edu.android;

import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author fabiomelillo@gmail.com
 *
 */
public class MovieArrayAdapter extends ArrayAdapter {
	
	Activity context;
	int listaDaCreare=0;

	
	public MovieArrayAdapter(Activity context, int layout, List<Movie> items){
		super(context, layout, items);
		this.context=context;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Movie m=(Movie) getItem(position);
		
		if(m==null)
			return null;
		View myview = convertView; //migliorare le prestazioni non ricaricando ogni volta
		
		if (myview==null){
			LayoutInflater inflater=context.getLayoutInflater();
			if (listaDaCreare==0)
				listaDaCreare=R.layout.movielist;
			myview = inflater.inflate(listaDaCreare, null);
			
		}
		
		
		TextView label = (TextView) myview.findViewById(R.id.labelMovie);
		label.setText(m.toString());
		
		ImageView visto=(ImageView) myview.findViewById(R.id.imageView1);
		ImageView hd=(ImageView) myview.findViewById(R.id.imageView2);
		ImageView stellina=(ImageView) myview.findViewById(R.id.imageView3);
		if (m.getSeen()>0){
			visto.setImageResource(android.R.drawable.checkbox_on_background);
		}
		if (m.getHd()<=0){
			hd.setImageDrawable(null);
		} else
			hd.setImageResource(R.drawable.ic_menu_hd);
		
		int voto=m.getRate();
		
		switch (voto) {
		case 1:
			stellina.setImageResource(R.drawable.stella_15);
			break;
		case 2:
			stellina.setImageResource(R.drawable.stella_25);
			break;
		case 3:
			stellina.setImageResource(R.drawable.stella_35);
			break;
		case 4:
			stellina.setImageResource(R.drawable.stella_45);
			break;
		case 5:
			stellina.setImageResource(R.drawable.stella_55);
			break;
		default:
			stellina.setImageDrawable(null);
			break;
		}
		
		return myview;
	}

}
